package cn.wolfcode.crm.web.controller;


import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class ExceptionsHandler {
    @ExceptionHandler(UnauthorizedException.class)//指定类型的异常
    public void exceptionHandler (HandlerMethod  method, HttpServletResponse response) throws Exception{

        ResponseBody anno = method.getMethodAnnotation(ResponseBody.class);
        if(anno != null){
            //{"success":false,"message":"你没有权限操作","total":0,"rows":[]}
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write("{\"success\":false,\"message\":\"你没有权限操作\",\"total\":0,\"rows\":[]}");

            //该请求方式为ajax
        }else{
            //普通的请求方式
            response.setContentType("text/html;charset=utf-8");
            response.sendRedirect("/nopermission.jsp");
        }
    }
}
